package team.nine.kuaichezuche.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import team.nine.kuaichezuche.model.VehicleInfo;
import team.nine.kuaichezuche.service.VehicleInfoService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 车辆信息控制器，处理车辆信息相关的HTTP请求
 */
@RestController
@RequestMapping("/api/cars")
public class VehicleInfoController {

    @Autowired
    private VehicleInfoService vehicleInfoService;

    /**
     * 获取车辆列表
     * @param page 页码，默认为1
     * @param pageSize 每页数量，默认为10
     * @return 包含车辆列表的响应
     */
    @GetMapping
    public ResponseEntity<Map<String, Object>> getCarList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        
        // 验证参数
        if (page < 1) {
            page = 1;
        }
        if (pageSize < 1) {
            pageSize = 10;
        } else if (pageSize > 10) {
            pageSize = 10; // 限制最多返回10辆车
        }

        // 调用服务层获取车辆列表
        List<Map<String, Object>> cars = vehicleInfoService.getVehicleInfoList(page, pageSize);
        int total = vehicleInfoService.getVehicleInfoCount();

        // 构建响应数据
        Map<String, Object> response = new HashMap<>();
        response.put("data", cars);
        response.put("total", total);
        response.put("page", page);
        response.put("pageSize", pageSize);
        response.put("success", true);

        return ResponseEntity.ok(response);
    }

    /**
     * 局部更新车辆信息（仅覆盖非空字段）
     */
    @PatchMapping("/{id}")
    public ResponseEntity<Map<String, Object>> patchVehicleInfo(
            @PathVariable Integer id,
            @RequestBody VehicleInfo payload) {
        Map<String, Object> response = new HashMap<>();
        try {
            if (id == null) {
                response.put("success", false);
                response.put("message", "车辆信息ID不能为空");
                return ResponseEntity.badRequest().body(response);
            }
            // 确保payload携带目标ID
            payload.setId(id);
            VehicleInfo updated = vehicleInfoService.updateVehicleInfo(payload);
            response.put("success", true);
            response.put("data", updated);
            return ResponseEntity.ok(response);
        } catch (IllegalArgumentException ex) {
            response.put("success", false);
            response.put("message", ex.getMessage());
            return ResponseEntity.badRequest().body(response);
        } catch (Exception ex) {
            response.put("success", false);
            response.put("message", "更新失败: " + ex.getMessage());
            return ResponseEntity.status(500).body(response);
        }
    }
}